package com.qf.smartdevice.service.impl;

import com.qf.smartdevice.cache.AreaCache;
import com.qf.smartdevice.constans.ResultCode;
import com.qf.smartdevice.exceptions.QueryDataException;
import com.qf.smartdevice.pojo.Area;
import com.qf.smartdevice.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class AreaServiceImpl implements IAreaService {
    @Autowired
    private AreaCache areaCache;

    @Override
    public List<Area> getAllParentArea() {
        List<Area> allDate = areaCache.getAllDate();//所有的区域数据
        // parentid 为空的就是父区域
        // parentid 为空的就是以及父区域,所以过滤时只需要判断 parentid==null
        List<Area> areaList=allDate.stream().filter(
                area -> area.getParentid()==null
        ).collect(Collectors.toList());
        return areaList;
    }

    @Override
    public List<Area> getAllAreasByParentId(Long parentId) {
        if (parentId==null||parentId<=0){
            throw new QueryDataException("主键不存在", ResultCode.DATA_NOT_EXIST);
        }
        List<Area> allDate = areaCache.getAllDate();
        //获取 parentid == 传递的 id 数据
        List<Area> areaList=allDate.stream().filter(
                area -> area.getParentid()==parentId
        ).collect(Collectors.toList());
        return areaList;
    }
}
